<template>
  <div class="padding-left-right-20 padding-top-20">
    <el-row type="flex" justify="end" style="margin-bottom: 15px">
      <el-form @submit.native.prevent style="flex: 1; margin-right: 10px">
        <el-input placeholder="请输入表单名称" v-model.trim="searchValue" @keyup.enter.native="selectList(1)" size="mini" />
      </el-form>
      <el-button type="primary" size="mini" @click="selectList(1)">查询</el-button>
      <el-button type="primary" size="mini" @click="addBtn">新增</el-button>
    </el-row>
    <el-row>
      <el-table :data="list" size="medium" @row-click="management" :border="false" :show-header="false">
        <el-table-column label="表单名称" align="left" prop="formName" />
        <el-table-column label="更改时间" width="180" align="right">
          <template slot-scope="scope">
            <span class="common-table-item">更改时间：{{ scope.row.updateTime | formatDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="right">
          <template slot-scope="scope">
            <el-button type="text el-icon-delete" @click.stop="deleteItem(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <el-pagination class="t-right margin-top-10" prev-text="上一页" next-text="下一页" layout="prev, pager, next" :total="queryParams.total" :current-page.sync="queryParams.currentPage" :page-size="queryParams.pageSize" @current-change="selectList" />
  </div>
</template>

<script>
  import { apiFormList, apiFormDelete } from '../../api/process';

  export default {
    data() {
      return {
        list: [],
        queryParams: {
          total: 0,
          pageSize: 20,
          currentPage: 1,
          formType: 1,
          formName: null,
          parentFormKey: 'null',
        },
        searchValue: '',
      };
    },
    mounted() {
      this.selectList(1);
    },
    methods: {
      async selectList(currentPage = 1) {
        this.queryParams.currentPage = currentPage || this.queryParams.currentPage;
        this.queryParams.formName = this.searchValue ? this.searchValue : null;
        const { rows, total } = await apiFormList(this.queryParams);
        this.list = rows;
        this.queryParams.total = total;
      },
      addBtn() {
        let pathUrl = {
          name: 'process-form-detail',
        };
        this.$router.push(pathUrl);
      },
      management(item) {
        let pathUrl = {
          name: 'process-form-detail',
          query: {
            primaryKey: item.primaryKey,
          },
        };
        let url = this.$router.resolve(pathUrl);
        window.open(url.href, '_blank');
      },
      //删除
      deleteItem(item) {
        this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            let res = await apiFormDelete(item.primaryKey);
            this.$message.success('删除成功!');
            setTimeout(() => {
              this.selectList();
            }, 500);
          })
          .catch(() => {});
      },
    },
  };
</script>
